# -*- coding:UTF-8 -*-
import pygame
import os
import actor
class AutoActor(actor.Actor):
    '''
    自动运动的角色
    '''
    def __init__(self, x, y , dir, name, type):
        '''
        初始化自动运动的角色
        :param x: x坐标
        :param y: y坐标
        :param dir: 方向
        :param name: 姓名
        :param type: 类型 character delegator king soldier trader
        '''
        super(AutoActor, self).__init__(x, y, dir, name)
        self.x = float(x)
        self.y = float(y)
        if type == 'character':
            self.rect.bottom = self.rect.top + 32
            self.rect.right = self.rect.left + 16
            self.x_vel = 1
            self.y_vel = 1
        else:
            self.rect.bottom = self.rect.top + 32
            self.rect.right = self.rect.left + 32
            self.x_vel = 0.4
            self.y_vel = 0.4

        self.type = type
        self.step_count = 0;

        if dir == 'lr':
            self.curr_dir = 'left'
        elif dir == 'ud':
            self.curr_dir = 'up'

    def set_imgae_path(self):
        '''
        设置图片位置
        :return:
        '''
        filename = self.type + '.png'
        self.path = os.path.join('resource','graphics','actor',filename)

    def init_rect(self):
        '''
        初始化角色自己
        :return:
        '''
        self.curr_img_id = 0;
        self.set_rect()
        self.image_pos = {'left': self.left_rect, 'right': self.right_rect, 'up': self.up_rect,
            'down': self.down_rect }

    def set_rect(self):
        '''
        设置图片位置
        :return:
        '''
        if self.type == 'character':
            self.left_rect = [pygame.Rect(0, 96, 16, 32),pygame.Rect(16,96,16,32),
                              pygame.Rect(32,96,16, 32),pygame.Rect(48,96, 16, 32)]  # 角色左方向图片位置
            self.right_rect = [pygame.Rect(0, 32, 16, 32),pygame.Rect(16,32,16, 32),
                               pygame.Rect(32,32,16, 32),pygame.Rect(48,32,16, 32)]  # 角色右方向图片位置
            self.down_rect = [pygame.Rect(0, 0, 16, 32),pygame.Rect(16,0,16, 32),
                              pygame.Rect(32,0,16, 32),pygame.Rect(48,0,16, 32)]  # 角色右方向图片位置
            self.up_rect = [pygame.Rect(0, 64, 16, 32),pygame.Rect(16,64,16, 32),
                            pygame.Rect(32,64,16, 32),pygame.Rect(48,64,16, 32)]  # 角色右方向图片位置
        else :
            self.left_rect = [pygame.Rect(0, 32, 32, 32), pygame.Rect(32, 32, 32, 32)]  # 角色左方向图片位置
            self.right_rect = [pygame.Rect(64, 32, 32, 32), pygame.Rect(96, 32, 32, 32)]  # 角色右方向图片位置
            self.down_rect = [pygame.Rect(64, 0, 32, 32), pygame.Rect(96, 0, 32, 32)]  # 角色右方向图片位置
            self.up_rect = [pygame.Rect(0, 0, 32, 32), pygame.Rect(32, 0, 32, 32)]  # 角色右方向图片位置

    def update(self):
        '''
        自动运行，更新自己的状态
        :return:
        '''
        if self.curr_dir == 'right':
            self.x += self.x_vel
            self.rect.left = int(self.x)
        elif self.curr_dir == 'left':
            self.x -= self.x_vel
            self.rect.left = int(self.x)
        elif self.curr_dir == 'down':
            self.y += self.y_vel
            self.rect.top = int(self.y)
        else:
            self.y -= self.y_vel
            self.rect.top = int(self.y)

        self.step_count += 1
        if self.step_count == 30:
            self.step_count = 0;

            if self.curr_dir == 'right':
                self.curr_dir = 'left'
            elif self.curr_dir == 'left':
                self.curr_dir = 'right'
            elif self.curr_dir == 'down':
                self.curr_dir = 'up'
            else:
                self.curr_dir = 'down'

        #移动图片位置
        if self.type == 'character':
            self.curr_img_id += 1
        elif self.step_count % 2 == 0:
            self.curr_img_id += 1

        if self.curr_img_id >= len(self.image_pos[self.curr_dir]):
            self.curr_img_id = 0
